// packageUser/pages/cash/cash.js
import WxValidate from '../../../plugins/wx-validate/WxValidate';
import WalletService from '../../../service/WalletService';
const app = getApp();
const rules = {
  money: {
    required: true
  }
};
const messages = {
  money: {
    required: '提现金额不能为空'
  }
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: app.globalData.user,
    available: 0,
    applySuccess: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.walletService = new WalletService();
    const available = options.available;
    if(available){
      this.setData({ available: available });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  /**
   * 全部提现
   */
  cashAll:function(){

  },
  formSubmit: function (e) {
    const _ = this;
    const wxValidate = new WxValidate(rules, messages);
    const cash = e.detail.value;
    if (!wxValidate.checkForm(cash)) {
      const error = wxValidate.errorList[0]
      this.setData({ errMsg: error.msg })
      return false
    }
    this.setData({ errMsg: '' })
    this.walletService.cashApply(cash).then(function (res) {
      console.log(res);
      if(res.status == 0){
        _.setData({applySuccess: true});
      }else{
        wx.showToast({
          title: res.message,
          duration: 2000
        })
      }
    });
  },
})